#ifndef _MUTEX_
#define _MUTEX_

#include <pthread.h>

class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&_mtx, nullptr);
    }

    void lock()
    {
        pthread_mutex_lock(&_mtx);
    }

    void unlock()
    {
        pthread_mutex_unlock(&_mtx);
    }

    ~Mutex()
    {
        pthread_mutex_destroy(&_mtx);
    }
private:
    pthread_mutex_t _mtx;
};

// 互斥锁的RAII模式
class MutexGuardian
{
public:
    MutexGuardian(Mutex& mtx)
    :_mtx(mtx)
    {
        _mtx.lock();
    }

    ~MutexGuardian()
    {
        _mtx.unlock();
    }
private:
    Mutex _mtx;
};

#endif